package com.cqc.manage.controller;


import cn.hutool.core.date.DateUtil;
import com.cqc.manage.dto.AmountBusinessDto;
import com.cqc.manage.dto.JsonResultDto;
import com.cqc.manage.service.Imp.business.BusinessEchartService;
import com.cqc.manage.service.Imp.business.CleanCache;
import com.cqc.manage.service.Imp.business.CostStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echart")
@Api(tags = "echart" , description = "图表数据")
public class EchartController {

    private Logger LOG = LoggerFactory.getLogger(EchartController.class);

    private int count = 1;

    @Autowired
    private CostStatisticsService costStatisticsService;
    @Autowired
    private BusinessEchartService businessEchartService;

    @Autowired
    private CleanCache cleanCache;

    String today= null;  // 缓存key


    @ApiOperation(value = "业务金额图表",notes = "业务金额图表")
    @RequestMapping(value = "/getEchartAmountBusiness",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getEchartAmountBusiness(){
        AmountBusinessDto engineerCost = costStatisticsService.getEngineerCost();
        return new JsonResultDto("success","200",engineerCost);
    }

    @ApiOperation(value = "echar主图数据",notes = "echar主图数据")
    @RequestMapping(value = "/getEchartMain",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getEchartMain(){
//        count++;
        today= DateUtil.today();
        Map<String, Map<Integer, List<Float>>> mainEchart = businessEchartService.getMainEchart(today);
        return new JsonResultDto("success","200",mainEchart);
    }


    @ApiOperation(value = "echar主图数据",notes = "echar主图数据-每个月的数据情况")
    @RequestMapping(value = "/getEchartMainEveryMonth",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getEchartMainEveryMonthAgreementAmount(){
        List<Float> everyMonthAgreementAmount = businessEchartService.getEveryMonthAgreementAmount();
        return new JsonResultDto("success","200",everyMonthAgreementAmount);
    }

    @ApiOperation(value = "echart每个公司的业务情况",notes = "echart每个公司的业务情况")
    @RequestMapping(value = "/getEchartCompany",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getEchartCompanyAgreementAmount(){
        List<Map<String, Integer>> pieCompanyStatistics = costStatisticsService.getPieCompanyStatistics();
        return new JsonResultDto("success","200",pieCompanyStatistics);
    }


    /**
     * 每天凌晨一点清空缓存
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanCache(){
        cleanCache.cleanCacheFuncation(this.today);
    }

    /**
     * 每天凌晨两点，计算
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void generateCache(){
        today= DateUtil.today();
        businessEchartService.getMainEchart(today);
    }

}
